package kylin.service.impl;

import kylin.dao.NetLinkedUserDao;
import kylin.service.NetLinkedUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @Title 用户Service层
 * @Description
 * @author XQL
 * @Date 2015年8月13日 下午4:07:01
 */
@Service
public class NetLinkedUserServiceImpl implements NetLinkedUserService {

	@Autowired
	private NetLinkedUserDao detLinkedUserDao;

	public void foreachUser(List<String> reList, StringBuffer sb_ids, int id, int flag){
		List<Integer> qList = detLinkedUserDao.getUser(id,flag);
		if(qList != null && qList.size() > 0){
			for(Integer t_id : qList){
				StringBuffer t_sb_ids = new StringBuffer(sb_ids);
				t_sb_ids.append("," + t_id);
				String temp = "," + sb_ids.toString() + ",";
				if(temp.contains("," + t_id + ",")){
					System.gc();
					reList.add(t_sb_ids.toString());
				}else{
					System.gc();
					foreachUser(reList,t_sb_ids,t_id,flag);
				}
			}
		}else{
			reList.add(sb_ids.toString());
		}
	}

	/**
	 * <p>Description : </p>
	 * <p>Author : XQL</p>
	 * <p>Date : 2016/1/30 17:31</p>
	 *
	 * 此方法回导致内存溢出
	 */
	public void foreachUser(List<List<Integer>> reList, List<Integer> list, int id, int flag){
		List<Integer> qList = detLinkedUserDao.getUser(id,flag);
		if(qList != null && qList.size() > 0){
			for(Integer t_id : qList){
				boolean sign = false;
				for(Integer t2_id : list){
					if(t2_id.equals(t_id)){
						sign = true;
						break;
					}
				}
				List<Integer> tempList = new ArrayList<Integer>(list);
				tempList.add(t_id);
				if(sign){
					reList.add(tempList);
				}else{
					foreachUser(reList,tempList,t_id,flag);
				}
			}
		}else{
			reList.add(list);
		}
	}
}
